#ifndef EXPERIMENT_H
#define EXPERIMENT_H

#include "blldataflow.h"
#include "sqlhelper.h"
#include "commonfunction.h"
#include <QDateTime>
#include <QTimer>
#include "exppass.h"
#include "expfail.h"
#include "bllcontroller.h"
#include "labelbutton.h"
#include "project.h"
#include <QBitmap>
#include <QtCharts>
#include "ivdspaint.h"
using namespace QtCharts;


namespace Ui {
class Experiment;
}

class CustomNumEdit;
class Experiment : public IVdsPaint
{
    Q_OBJECT

public:
    explicit Experiment(QWidget *parent = nullptr);
    ~Experiment();

private slots:
    void changeExperimentPageIndexSlot(int index);

    void updateExpDuration();
    void updateSampleDetail(Sample p);
    void updateMaterialDetail(Material p);
    void updateParameterDetail(Parameter p);

    void on_btnStartExp_clicked();
    void on_btnEndExp_clicked();

    void on_btnScanSample_clicked();
    void on_btnScanMaterial_clicked();
    void on_btnClearSample_clicked();
    void on_btnClearVolume_clicked();
    void on_btnClearMaterial_clicked();

    void on_btnPreviewReport_clicked();
    void on_btnContinueExp_clicked();

    void expPassSlot(const HostReply &data);
    void expFailSlot(const HostReply &data);

    void liquidDetectedSlot();
    void experimentStartSlot(const HostReply &result);
    void insertExpInfoSlot(const ExperimentInfo &expInfo);

    //SHK_Result
    void shkCheckResultSlot(const HostReply &result);
    void setBtnParamCheckedSlot(bool checked);
    void setBtnParamChecked(LabelButton * btnLabel, bool checked);
    void loadEnabledParamters();
    void retranslateUiSlot();

public slots:
    void onPaintChart(const VdsMeasureDataPtr data) Q_DECL_OVERRIDE;
    void onMeasureError(VdsError vdsError) Q_DECL_OVERRIDE;
    void onDataFileReady(const QString &path) Q_DECL_OVERRIDE;

protected:
    bool eventFilter(QObject *, QEvent *) Q_DECL_OVERRIDE;
    void showEvent(QShowEvent *) Q_DECL_OVERRIDE;
    void hideEvent(QHideEvent *) Q_DECL_OVERRIDE;

private:
    void changeFocus(QWidget *edit);
    void selectEditAll(QLineEdit *edit);
    void selectEditAll(CustomNumEdit *edit);
    void initWidget();
    void initBtnParam();
    void clearParamsSelect();

    void paintDivider(QWidget *);
    void paintWave();

    void autosetAxiesRange();
    void initGraphs();
    void clearGraphs();
    void buildMessage(const QString &message);
    void buildGraphs(const double *data, int length);
    void buildExpEndChart();
    void startComponent();
    void stopComponent();
    void registerMetaType();
    void setVdsParameter();
    void start();
    void stop(bool);
    bool fillMaterialInfo();

    void clearInput();
    bool checkInput();
    EttaError checkParamSelect();
    EttaError checkSampleName();
    EttaError checkMaterial();
    EttaError checkSampleVolume();

    void initExperimentInfo();
    void updateExperimentInfo(bool success);

    void sendStartCmd();
    void sendContinueCmd();
    void sendStopCmd();
    void sendAutoCmd();

    void setMaterialUsed();

private:
    Ui::Experiment *ui;
    BllDataFlow * bdf;
    SqlHelper * sqlHelper;
    Parameter parameter;
    Sample sample;
    Material material;
    QDateTime expStartTime;
    QDateTime expEndTime;
    ExperimentInfo expInfo;
    QDateTime expStartDateTime;
    QDateTime expEndDateTime;
    QTimer * expDurationTimer;
    QTimer * expDurationPaintTimer;
    int progressCount;
    BllController * bCtrl;

    QString enableStr;
    QString disableStr;
    QString btnLabelStyle;
    QString labelNameStyleUnchecked;
    QString labelCodeStyleUnchecked;
    QString labelNameStyleChecked;
    QString labelCodeStyleChecked;
    QValueAxis * xAxisExp; //轴
    QValueAxis * yAxisExp;
    QValueAxis * xAxisExpEnd;
    QValueAxis * yAxisExpEnd;
    QLineSeries * seriesExp; //曲线
    QLineSeries * seriesExpEnd;
    QChart * chartExp; // 图表
    QChart * chartExpEnd;
    double chartSampleTime; // 示波器采样时间
    quint64 duration; // 预计运行时长
    bool manualStop;
    bool reuseSample;
    bool continueExp;
    bool autoStop;
    bool liquidDetected;
    int maxRuntime; // 单位：s
};

#endif // EXPERIMENT_H
